<?php 
/**
 * 
 */
 class ArticleController extends AuthController{
 	
 	private $_db;
	private $_category;
	public function __init(){
		parent::__init();
		$this->_db=K('Article');
		$this->_category=F("category");
	}
    public function index(){
    	$cid = Q('get.cid',0,'intval');
    	if($cid){
    		$page = new Page($this->_db->where(array('cid' => $cid))->count(),20);
			$this->page =$page->show();
			$this->article=$this->_db->where(array('cid' => $cid))->limit($page->limit())->order('id desc')->all();
			$this->display();
    	}else{
    		$page = new Page($this->_db->count(),20);
			$this->page =$page->show();
			$this->article=$this->_db->limit($page->limit())->order('id desc')->all();
			$this->display();
    	}
    }
	public function add(){
		if(IS_POST){
			if($this->_db->add_article()){
				$this->success('添加成功','index');
			}else{
				$this->error('文章添加失败'.$this->_db->error);
			}
		}else{
			$this->category=$this->_category;
			$this->display();
		}
	}
	public function edit(){
		$id=Q("id",NULL,'intval');
		if(!$id)$this->error('参数传递错误！');
		if(IS_POST){
			if($this->_db->edit_article()){
				$this->success('修改成功','index');
			}else{
				$this->error('文章修改失败'.$this->_db->error);
			}
		}else{
			$this->field = $this->_db->find($id);
			$this->category=$this->_category;
			$this->display();
		}
	}
	public function del() {
		if($this->_db->del_article()){
			$this->success('文章删除成功','index');
		}else{
			$this->error('文章删除失败');
		}
	}
 } ?>